<?php

require_once ROOT_APPLICATION_MODULES_END_FORMS.DS."Category.php";
require_once ROOT_APPLICATION_MODULES_END_OBJECTS.DS.'Category.php';
require_once ROOT_APPLICATION_MODULES_END_MODELS.DS.'Categorys.php';

class IndexController extends Zend_Controller_Action
{
	
    public function init()
    {
        /* Initialize action controller here */
    }
    
    public function addAction()
    {   
    	//Create form enter info for a new category
    	$form = new Application_Form_Categorys();
    	
    	//Set label for the button
        $form->submit->setLabel('Add');
        
        //Set client view page form  = $form
        $this->view->form = $form;
        
        //If can get client request 
        if ($this->getRequest()->isPost()) 
        {
        	//Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData))   //If data is valid 
            {
            	//Get data one by one
                $name = $form->getValue('name');
                $slug = $form->getValue('slug');
                $meta = $form->getValue('meta');
                $left_id = $form->getValue('left_id');
                $right_id = $form->getValue('right_id');
                $parent_id = $form->getValue('parent_id');
                $num_views = $form->getValue('num_views');
                $is_active = $form->getValue('is_active');
                $created_date = $form->getValue('created_date');
                $modified_date = $form->getValue('modified_date');
                $user_id = $form->getValue('user_id');
                $language = $form->getValue('language');
                
                //Create new Category object from datas
                $category = new Category(null, $name, $slug, $meta, $left_id, $right_id, $parent_id, $num_views, $is_active, $created_date, $modified_date, $user_id, $language);
                
                //Create Category model 
                $categorys = new End_Models_Categorys();
                
                //Use insert function from Category model
                $categorys->_insert($category);
                
                //Move to index page
                $this->_helper->redirector('index');
            } else 
            {
            	//Return form
                $form->populate($formData);
            }
        }
    }
    
    public function deleteAction()
    {	
    	if ($this->getRequest()->isPost()) 
    	{
            $del = $this->getRequest()->getPost('del'); 
    		if ($del == 'Yes') 
            {
                $category_id = (int)$this->getRequest()->getPost('category_id');
                $categorys = new End_Models_Categorys();
                $categorys->_delete($category_id);
            }
            $this->_helper->redirector('index');
        } else 
        {
            $category_id = $this->_getParam('category_id', 0);
            $categorys = new End_Models_Categorys();
            $this->view->category = Utility_Converter::objectToArray($categorys->_find($category_id));
        }
    }
    
    public function editAction()
    {
    	$form = new Application_Form_Categorys();
        $form->submit->setLabel('Save');
        $this->view->form = $form;
       
        if ($this->getRequest()->isPost()) 
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) 
            {
                $category_id = (int)$form->getValue('category_id');
                $name = $form->getValue('name');
                $slug = $form->getValue('slug');
				$meta = $form->getValue('meta');				//lay thong tin
				$category = new Category($category_id, $name, $slug, $meta);	//dua thong tin vao doi tuong
                $categorys = new End_Models_Categorys();						//tao doi tuong model voi cac phuong thuc xu li doi tuong o tren
                $categorys->_update($category);								//xu li doi tuong voi phuong thuc cua model
                $this->_helper->redirector('index');				//chuyen den trang index
            } else 
            {
                $form->populate($formData);
            }
        } else 
        {
            $category_id = $this->_getParam('category_id', 0);
            if ($category_id > 0) 
            {
                $categorys = new End_Models_Categorys();
                $form->populate(Utility_Converter::objectToArray($categorys->_find($category_id)));
            }
        }
    }
    
    public function indexAction()
    {
    	$modelCategorys = new End_Models_Categorys();
		$categorys = $modelCategorys->fetchAll();
		$this->view->Categorys = $categorys;
    }
    
}







